<?php


namespace App\Models;


use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;

/**
 * Class OrderShipment
 * @property Order $order
 * @property Collection $pickingDetail
 * @property Collection $products
 * @package App\Models
 * @author luffyzhao@vip.126.com
 */
class OrderShipment extends Model
{
    protected $table = 'order_shipment';

    protected $fillable = ['customer_id', 'info'];

    /**
     * @param Collection $values
     */
    protected function setInfoAttribute(Collection $values)
    {
        $this->attributes['info'] = $values->sortBy('product_id')->map(function ($item){
            return $item['product_id'] . '*' . $item['quantity'];
        })->implode("|");

        $this->attributes['remark'] = $values->pluck('product_id')->sort()->implode("|");

        $this->attributes['type'] = $values->sum('quantity') === 1 ? 0 : 1;
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     * @author luffyzhao@vip.126.com
     */
    public function order(){
        return $this->belongsTo(Order::class);
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function customer()
    {
        return $this->belongsTo(Customer::class);
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function pickingDetail(){
        return $this->hasMany(PickingDetail::class, 'order_id', 'order_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     * @author luffyzhao@vip.126.com
     */
    public function logs(){
        return $this->hasMany(OrderShipmentLog::class);
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     * @author luffyzhao@vip.126.com
     */
    public function config(){
        return $this->hasMany(OrderConfig::class, 'order_id', 'order_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     * @author luffyzhao@vip.126.com
     */
    public function products(){
        return $this->hasMany(OrderProduct::class, 'order_id', 'order_id');
    }
}
